﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Data;

namespace ZhiHeng.OneCard.WpfClient.Common
{
    [ValueConversion(typeof(string), typeof(String))]
    public class PriceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string str = "";
            try
            {
                decimal dd=System.Convert.ToDecimal(value) * 0.01m;
                str = dd.ToString("C");
            }
            catch(Exception ex)
            {
                return "";
            }
           
            return str;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double num = 0;
            Match mc = Regex.Match(value.ToString(), "\\d+(\\.\\d+)");
            if(double.TryParse(mc.Groups[0].ToString(),out num))
            {
                return num * 100;
            }else
            {
                return value;
            }
        }
    }
}
